package com.my.screenadapter.time;

import android.app.DatePickerDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import com.my.screenadapter.Base.BaseActivity;
import com.my.screenadapter.R;

import java.util.Calendar;

import butterknife.BindView;

public class MyTimeActivity extends BaseActivity {

@BindView(R.id.btn_month)
Button btnMonth;
@BindView(R.id.tv_month)
TextView tvMonth;
    @Override
    protected void init() {
        final Calendar c = Calendar.getInstance();

        mYear = c.get(Calendar.YEAR);
        mMonth = 0;
        mDay = 1;
        dpdStart = new DatePickerDialog(mContext,
                mDateSetListener_start, mYear, mMonth, mDay);
    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_my_time;
    }

    private int mYear;
    private int mMonth;
    private int mDay;
    /* 日期框1 */
    private DatePickerDialog dpdStart;
    public void choiceMonth(View view) {
        dpdStart.show();
        DatePicker dp = findDatePicker((ViewGroup) dpdStart.getWindow()
                .getDecorView());
        dp.setCalendarViewShown(false);
        if (dp != null) {
            ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0))
                    .getChildAt(2).setVisibility(View.GONE);
        }

    }

    /**
     * 第一个日期控件的事件
     */
    private DatePickerDialog.OnDateSetListener mDateSetListener_start = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;
            Log.v("===", "mYear:" + mYear);
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            updateDateDisplay_start();
        }
    };

    private DatePicker findDatePicker(ViewGroup group) {
        if (group != null) {
            for (int i = 0, j = group.getChildCount(); i < j; i++) {
                View child = group.getChildAt(i);
                if (child instanceof DatePicker) {
                    return (DatePicker) child;
                } else if (child instanceof ViewGroup) {
                    DatePicker result = findDatePicker((ViewGroup) child);
                    if (result != null)
                        return result;
                }
            }
        }
        return null;
    }

    /**
     * 第一个时间文本框更新日期显示
     */
    private void updateDateDisplay_start() {
        StringBuilder append = new StringBuilder().append(mYear).append("-")
                .append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1));
        tvMonth.setText(append.toString());

    }
}
